The Data

In a previous analysis, I explored publicly available data from the Bureau of Meteorology to examine change in Australian temperatures over the past 100 years. In this page we will utilize the power off ggplot2 to visualise the data

Let’s load our dependencies

require(tidyverse)
require(readr)
require(ggplot2)
require(gganimate)
require(viridis)
require(scales)
require(gifski)
require(png)
require(transformr)
require(kableExtra)

& read-in the pre-cleaned temperature data from the previous kernel

tbl <- read.csv("/Users/perkot/GIT/data/ACORN-SAT-Clean.csv")
site.name date minimum.temperature..degC. maximum.temperature..degC. Year
KALUMBURU 1941-09-01 20.6 29.8 1941
KALUMBURU 1941-09-02 20.6 29.8 1941
KALUMBURU 1941-09-03 19.6 29.3 1941
KALUMBURU 1941-09-04 21.2 37.1 1941
KALUMBURU 1941-09-05 19.8 30.9 1941
KALUMBURU 1941-09-06 19.8 NA 1941
KALUMBURU 1941-09-07 NA NA 1941
KALUMBURU 1941-09-08 19.8 32.0 1941
KALUMBURU 1941-09-09 21.3 32.7 1941
KALUMBURU 1941-09-10 21.3 33.4 1941

If using a mac, you may encounter some teething issues rendering visualizations with gganimate. This resource provides a guide of steps to troubleshoot in terminal. Otherwise, we should be good-to-go.

Monthly avg maximum temperatures

Animated by year

In the below step, I create customer colour palettes to give full manual control of which colours are assigned to which element in the visualisation (in the below example, state of Australia)

# color themes 
state.colors.gradient.2 <- 
  c("NT" = "#993B37", 
    "TAS" = "#7AA2BE", 
    "VIC" = "#4EA599",  
    "SA" = "#E1A345", 
    "NSW" = "#D2AF47", 
    "WA" = "#C88370", 
    "QLD" = "#CA6A33") 

state.colors.gradient.3 <- 
  c("NT" = "#993B37", 
    "TAS" = "#7AA2BE", 
    "VIC" = "#4ea58e",  
    "SA" = "#e3963d", 
    "NSW" = "#D2AF47", 
    "WA" = "#d97529", 
    "QLD" = "#c95042") 

To avoid repetition in our code, we can pre-save some of the custom theme elements to call in each visualisation (i.e. font sizes, font colours, legend formatting)

# Themes for plot visualisations 
theme_plot <-
  theme(
    plot.title = element_text(size = 14, hjust = 0, colour = "#4E4F4E", face = "bold"),
    plot.subtitle = element_text(size = 12, hjust = 0, colour = "#4E4F4E"),
    axis.title = element_text(size = 12, colour = "#4E4F4E"),
    legend.title = element_text(size = 12, colour = "#4E4F4E"),
    axis.text = element_text(size = 12, colour = "#4E4F4E"),
    panel.background = element_rect(fill = "#fcf9f0",
                                    colour = "#fcf9f0"),
    plot.background = element_rect(fill = "#fcf9f0",
                                   colour = "#fcf9f0"))

theme_plot_2 <-
  theme(
    plot.title = element_text(size = 12, hjust = 0, colour = "#4E4F4E", face = "bold"),
    plot.subtitle = element_text(size = 10, hjust = 0, colour = "#4E4F4E"),
    axis.title = element_text(size = 9, colour = "#4E4F4E"),
    legend.title = element_text(size = 10, hjust = 0.5, colour = "#4E4F4E"),
    axis.text = element_text(size = 9, colour = "#4E4F4E"),
    panel.background = element_rect(fill = "#fcf9f0",
                                    colour = "#fcf9f0"),
    plot.background = element_rect(fill = "#fcf9f0",
                                   colour = "#fcf9f0"))

For the first visualization, the average maximum temperature will be visualized for each state of Australia. This will be animated over each decade from 1900 up to 2010

As such the first required step is to group the dataframe by both state and decade, summarizing by the mean maximum temperature

create plot

# PLOT
state.plot.2 <- 
  tbl %>% 
  group_by(State, Era) %>% # group data by state, decade
  filter(Era != 2019) %>% # filter year with incomplete data
  summarise(avgmax = mean(maximum.temperature..degC., na.rm = TRUE)) %>% # calculate mean 
  ggplot(aes(Era, avgmax, group = State, color = State)) + # plot
  geom_line(size = 1.6) + 
  geom_segment(aes(xend = 2018, yend = avgmax), linetype = 2, colour = 'grey') + 
  geom_point(size = 8) + 
  geom_text(aes(x = 2018, label = State, size = 16), hjust = 0) + 
  scale_colour_manual(values = state.colors.gradient.2) +
  coord_cartesian(clip = 'off') + 
  labs(title = 'Average daily-maximum temperature in Australia over the past 100 years',
       subtitle = 'Separated by State',
       caption  = "Data Source: ACORN-SAT",
       y = 'Temperature (°C)',
       X = "Year") +
  theme_minimal() + 
  theme(legend.position = "none") +
  theme(plot.margin = margin(5.5, 40, 5.5, 5.5)) + 
  theme_plot +
  transition_reveal(Era) 
## `summarise()` has grouped output by 'State'. You can override using the `.groups` argument.
# ANIMATE PLOT
state.plot.2.anim <-
animate(state.plot.2,
        end_pause = 80,
        fps = 30,
        nframe = 240,
        height = 1024,
        width = 768)

All Seasons

All Seasons

Summer

Summer

Winter

Winter

Autumn

Autumn

Spring

Spring